﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MobileAPI.Web.Data;
using MobileAPI.BLL;
using MobileAPI.Entity.Model;
using MobileAPI.Common;

namespace MobileAPI.Web.APIs
{
    [MobileAPI(Code = "MC0004", Name = "获取报修详情")]
    public class GetRepairsDetails : IProcessMessage
    {
        private BLLRepairsInfo bll;
        public BLLRepairsInfo Bll
        {
            get
            {
                if (bll == null)
                    bll = BLLFactory.getInstance().CreateRepairsInfoBLL();
                return bll;
            }
        }


        #region IProcessMessage 成员

        public MobileRspMsg ProcessMessage(MobileReqMsg reqMsg)
        {
            try
            {
                MobileRspMsg rspMsg = new MobileRspMsg(reqMsg);
                // 获取参数字段（具体请参见文档）
                string RepairsId = reqMsg.Body.GetFieldValue("RepairsId");
                string RepairsDate = reqMsg.Body.GetFieldValue("RepairsDate");

                if (string.IsNullOrEmpty(RepairsId) || string.IsNullOrEmpty(RepairsDate))
                {
                    rspMsg.Head.RspCode = MessageCode.Paramter_Empty.Code;
                    rspMsg.Head.RspMsg = MessageCode.Paramter_Empty.Msg;
                    return rspMsg;
                }

                ERepairsInfo details = Bll.GetRepairDetails(RepairsId, RepairsDate);

                if (details == null)
                {
                    rspMsg.Head.RspCode  = MessageCode.Result_Empty.Code;
                    rspMsg.Head.RspMsg = MessageCode.Result_Empty.Msg;

                    return rspMsg;
                }
                else
                {
                    rspMsg.Body.AddField("SystemId", details.SystemId.TString());
                    rspMsg.Body.AddField("CustomerArea", details.CustomerArea);
                    rspMsg.Body.AddField("CustomerName", details.CustomerName);
                    rspMsg.Body.AddField("Diagnosis", details.Diagnosis);
                    rspMsg.Body.AddField("Flag", details.Flag.TString());
                    rspMsg.Body.AddField("Instrument", details.Instrument);
                    rspMsg.Body.AddField("InstrumentID", details.InstrumentID);
                    rspMsg.Body.AddField("IsStop", details.IsStop);
                    rspMsg.Body.AddField("Priority", details.Priority);
                    rspMsg.Body.AddField("Progress", details.Progress);
                    rspMsg.Body.AddField("RepairsDate", details.RepairsDate);
                    rspMsg.Body.AddField("RepairsId", details.RepairsId);
                    rspMsg.Body.AddField("RepairsPhone", details.RepairsPhone);
                    rspMsg.Body.AddField("RepairsStaff", details.RepairsStaff);
                    rspMsg.Body.AddField("RoomName", details.RoomName);

                    rspMsg.Body.AddField("WorkOrderId", details.WorkOrder.WorkOrderId);
                    rspMsg.Body.AddField("WorkOrderStaff", details.WorkOrder.WorkOrderStaff);
                    rspMsg.Body.AddField("WorkOrderType", details.WorkOrder.WorkOrderType);
                    rspMsg.Body.AddField("ServiceType", details.WorkOrder.ServiceType);
                    rspMsg.Body.AddField("Assistant", details.WorkOrder.Assistant);
                    rspMsg.Body.AddField("Remarks", details.WorkOrder.Remarks);

                    if (details.RepairsStateList != null)
                    {
                        var fl = rspMsg.Body.AddFieldList("RepairsStateList");
                        for (int i = 0; i < details.RepairsStateList.Count; i++)
                        {
                            var fll = fl.AddFieldList("RepairsState");
                            fll.AddField("DisposeCondition", details.RepairsStateList[i].DisposeCondition);
                            fll.AddField("OperatingDate", details.RepairsStateList[i].OperatingDate);
                            fll.AddField("OperatingTime", details.RepairsStateList[i].OperatingTime);
                            fll.AddField("OperatingType", details.RepairsStateList[i].OperatingType);
                        }
                    }
                    rspMsg.Head.RspCode = MessageCode.Success.Code;
                    rspMsg.Head.RspMsg = MessageCode.Success.Msg;

                    return rspMsg;
                }
            }
            catch (Exception e)
            {
                ErrorLogWriter.Write("Login-" + e);
                throw e;
            }

        }
        #endregion
    }
}